/*
 * 
 * 
 * 
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Order;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.ReviewDao;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.entity.Review;
import org.springframework.stereotype.Repository;

import javax.persistence.FlushModeType;
import javax.persistence.criteria.*;
import java.util.List;

/**
 * Dao - 评论
 */
@Repository("reviewDaoImpl")
public class ReviewDaoImpl extends BaseDaoImpl<Review, Long> implements ReviewDao {

    @Override
    public Double serviceScore(Member member) {
        if (member == null) {
            return 0.0;
        }
        String jpql = "select avg(review.serviceScore) from Review review where review.teacher = :teacher";
        Double totalScore = entityManager.createQuery(jpql, Double.class).setFlushMode(FlushModeType.COMMIT).setParameter("teacher", member).getSingleResult();
        return totalScore != null ? totalScore : 0.0;
    }

    @Override
    public Double schemeScore(Member member) {
        if (member == null) {
            return 0.0;
        }
        String jpql = "select avg(review.schemeScore) from Review review where review.teacher = :teacher";
        Double totalScore = entityManager.createQuery(jpql, Double.class).setFlushMode(FlushModeType.COMMIT).setParameter("teacher", member).getSingleResult();
        return totalScore != null ? totalScore : 0.0;
    }

    @Override
    public Double feedbackScore(Member member) {
        if (member == null) {
            return 0.0;
        }
        String jpql = "select avg(review.feedbackScore) from Review review where review.teacher = :teacher";
        Double totalScore = entityManager.createQuery(jpql, Double.class).setFlushMode(FlushModeType.COMMIT).setParameter("teacher", member).getSingleResult();
        return totalScore != null ? totalScore : 0.0;
    }

    public Page<Review> myCommemtPage(Pageable pageable, Member member) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        criteriaQuery.where(restrictions);
        criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createDate")));
        return super.findPage(criteriaQuery, pageable);
    }

    @Override
    public List<Review> myCommentList(Member teacher, Integer count, List<Filter> filters, List<Order> orders) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (teacher != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("teacher"), teacher));
        }
        criteriaQuery.where(restrictions);
        criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createDate")));
        return super.findList(criteriaQuery, null, count, filters, orders);
    }

    @Override
    public Page<Review> reviewPage(Pageable pageable, Member teacher) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (teacher != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("teacher"), teacher));
        }
        criteriaQuery.where(restrictions);
        return super.findPage(criteriaQuery, pageable);
    }

    @Override
    public long myReviews(Member teacher) {
        if (teacher == null) {
            return 0L;
        }
        String jpql = "select count(*) from Review review where review.teacher = :teacher";
        Long totalScore = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("teacher", teacher).getSingleResult();
        return totalScore != null ? totalScore : 0L;
    }

    public List<Review> findList(Member member, Product product, Review.Type type, Boolean isShow, Integer count, List<Filter> filters, List<Order> orders) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        if (product != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
        }
        if (type == Review.Type.positive) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
        } else if (type == Review.Type.moderate) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
        } else if (type == Review.Type.negative) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.findList(criteriaQuery, null, count, filters, orders);
    }

    public Page<Review> findPage(Member member, Product product, Review.Type type, Boolean isShow, Pageable pageable) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        if (product != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
        }
        if (type == Review.Type.positive) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
        } else if (type == Review.Type.moderate) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
        } else if (type == Review.Type.negative) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.findPage(criteriaQuery, pageable);
    }

    public Page<Review> findPageGroup(Member member, Product product, Review.Type type, Boolean isShow, Pageable pageable) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        pageable.getFilters().add(Filter.eq("product.goods",product.getGoods()));
        if (type == Review.Type.positive) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
        } else if (type == Review.Type.moderate) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
        } else if (type == Review.Type.negative) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.findPage(criteriaQuery, pageable);
    }

    public Long count(Member member, Product product, Review.Type type, Boolean isShow) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        if (product != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
        }
        if (type == Review.Type.positive) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
        } else if (type == Review.Type.moderate) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
        } else if (type == Review.Type.negative) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.count(criteriaQuery, null);
    }

    public boolean isReviewed(Member member, Product product) {
        if (member == null || product == null) {
            return false;
        }
        String jqpl = "select count(*) from Review review where review.member = :member and review.product = :product";
        Long count = entityManager.createQuery(jqpl, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("member", member).setParameter("product", product).getSingleResult();
        return count > 0;
    }

    public long calculateTotalScore(Product product) {
        if (product == null) {
            return 0L;
        }
        String jpql = "select sum(review.score) from Review review where review.product = :product and review.isShow = :isShow";
        Long totalScore = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("product", product).setParameter("isShow", true).getSingleResult();
        return totalScore != null ? totalScore : 0L;
    }

    public long calculateScoreCount(Product product) {
        if (product == null) {
            return 0L;
        }
        String jpql = "select count(*) from Review review where review.product = :product and review.isShow = :isShow";
        return entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("product", product).setParameter("isShow", true).getSingleResult();
    }

    @Override
    public List<Review> lastThreeReviews(Product product) {
        String queryStr = "SELECT review FROM Review As review " +
                "WHERE review.product=:product " +
                "ORDER BY review.createDate DESC ";
        return entityManager.createQuery(queryStr).setParameter("product", product).setMaxResults(3).getResultList();
    }

    @Override
    public long count(Member member, Product product, Boolean isShow) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
        Root<Review> root = criteriaQuery.from(Review.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        if (product != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.count(criteriaQuery, null);
    }

}